package com.lucksoft.map.sdk.gaode.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lucksoft.core.response.ApiResult;
import com.lucksoft.core.constant.BaseOperateConstant;
import com.lucksoft.map.sdk.gaode.param.MapSdkGaodeParam;
import com.lucksoft.map.sdk.gaode.service.MapSdkGaodeService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 高德地图路线规划接口控制器
 * 
 * @author Qwen
 * @date 2025/10/9
 * @version 1.0.0
 */
@Tag(name = "高德地图路线规划接口")
@RestController
@RequestMapping("/map/sdk/gaode/mapSdkGaode")
public class MapSdkGaodeController {
    
    private static final Logger logger = LoggerFactory.getLogger(MapSdkGaodeController.class);
    
    @Autowired
    private MapSdkGaodeService mapSdkGaodeService;
    
    /**
     * 获取高德地图路线规划信息
     * 
     * @param request 路线规划请求参数
     * @return ApiResult 路线规划结果
     */
    @Operation(summary = "获取高德地图路线规划信息")
    @PostMapping("/noGetwayReadRoute")
    public ApiResult getGaodeRoute(@RequestBody MapSdkGaodeParam request) {
        try {
            // 获取请求参数
            if (request == null) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "请求参数不能为空", null);
            }
            
            String origin = request.getOrigin();
            String destination = request.getDestination();
            String routeType = request.getRouteType() != null ? request.getRouteType() : "driving";
            String tactics = request.getTactics() != null ? request.getTactics() : "0";
            String ak = request.getAk();
            
            // 参数验证
            if (origin == null || origin.trim().isEmpty() || 
                destination == null || destination.trim().isEmpty()) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "起点和终点不能为空", null);
            }
            
            // 验证API Key
            if (ak == null || ak.trim().isEmpty()) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "高德地图API Key不能为空", null);
            }
            
            // 记录请求日志
            logger.info("高德地图路线规划接口请求: {}", request.toString());
            
            // 调用服务获取路线信息
            ApiResult result = mapSdkGaodeService.getRouteInfo(origin, destination, routeType, tactics, ak);
            
            if (result.getCode() == BaseOperateConstant.SUCCESS_CODE) {
                // 记录成功响应日志
                logger.info("高德地图路线规划接口响应成功");
            } else {
                // 记录失败响应日志
                logger.error("高德地图路线规划接口响应失败: {}", result.getMsg());
            }
            
            return result;
            
        } catch (Exception e) {
            // 记录错误日志
            logger.error("高德地图路线规划接口异常: {}", e.getMessage(), e);
            
            return new ApiResult(BaseOperateConstant.FAILURE_CODE, "服务内部错误: " + e.getMessage(), null);
        }
    }
}